package com.longdaji.decoration.ui.activitiesOfMine.pendingDelivery;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/8 0008.
 */

public class PendingDeliveryPresenter extends RxPresenter<PendingDeliveryContract.View> implements PendingDeliveryContract.Presenter {

    private DataManager mDataManager;
    private Account account;

    @Inject

    public PendingDeliveryPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
        account = Account.getInstance();
    }

    @Override
    public void initData() {
        doGetOrder();
    }

    private void doGetOrder() {
        final List<Orders.OrdersBean.OrderBean> pendingDeliveryOrderList = new ArrayList<>();
        addSubscribe(mDataManager.doGetOrderApi(account.getUserid(), null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(final Orders orders) {
                        LogUtil.d("succeed : doGetOrder ");
                        Observable.create(new ObservableOnSubscribe<List<Orders.OrdersBean.OrderBean>>() {
                            @Override
                            public void subscribe(ObservableEmitter<List<Orders.OrdersBean.OrderBean>> e) throws Exception {
                                for (Orders.OrdersBean.OrderBean orderBean : orders.getOrders().getOrder()) {
                                    if (Constants.ORDER_PENDING_DELIVERY == orderBean.getOrderStatus()) {
                                        pendingDeliveryOrderList.add(orderBean);
                                    }
                                }
                                e.onNext(pendingDeliveryOrderList);
                            }
                        }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<List<Orders.OrdersBean.OrderBean>>() {
                                    @Override
                                    public void accept(List<Orders.OrdersBean.OrderBean> pendingDeliveryOrderList) throws Exception {
                                        mView.showPendingDeliveryOrders(pendingDeliveryOrderList);
                                    }
                                });

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);
                    }
                }));
    }

    @Override
    public void onCancelOrderClick(final String userid, final String orderNo) {
        addSubscribe(mDataManager.doCancelOrderApi(userid, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : onCancelOrderClick ");
                        // TODO: 2018/4/17 0017 refresh pending delivery order list
                        doRefund(userid, orderNo);
                        doGetOrder();
                        mView.showCancelOrderSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }

    private void doRefund(String userid, String orderNo) {
        addSubscribe(mDataManager.doRefundApi(userid, orderNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed : doRefundApi ");
                        // TODO: 2018/5/1 0001 如果取消订单成功但是退款失败了呢
                        mView.showRefundSucceed();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed : " + msg);

                    }
                }));
    }
}
